package com.codeplay.controller;

import com.codeplay.entity.LearningProgress;
import com.codeplay.service.LearningProgressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/learning-progress")
@CrossOrigin(origins = "*")
public class LearningProgressController {

    private final LearningProgressService learningProgressService;

    @Autowired
    public LearningProgressController(LearningProgressService learningProgressService) {
        this.learningProgressService = learningProgressService;
    }

    @PostMapping
    public ResponseEntity<LearningProgress> createLearningProgress(@RequestBody LearningProgress learningProgress) {
        return ResponseEntity.ok(learningProgressService.createLearningProgress(learningProgress));
    }

    @GetMapping("/{id}")
    public ResponseEntity<LearningProgress> getLearningProgressById(@PathVariable Long id) {
        return ResponseEntity.ok(learningProgressService.getLearningProgressById(id));
    }

    @GetMapping("/user/{userId}/course/{courseId}")
    public ResponseEntity<List<LearningProgress>> getLearningProgressByUserAndCourse(
            @PathVariable Long userId, @PathVariable Long courseId) {
        return ResponseEntity.ok(learningProgressService.getLearningProgressByUserAndCourse(userId, courseId));
    }

    @GetMapping("/user/{userId}/content/{contentId}")
    public ResponseEntity<LearningProgress> getLearningProgressByUserAndContent(
            @PathVariable Long userId, @PathVariable Long contentId) {
        return ResponseEntity.ok(learningProgressService.getLearningProgressByUserAndContent(userId, contentId));
    }

    @GetMapping("/user/{userId}/chapter/{chapterId}")
    public ResponseEntity<List<LearningProgress>> getLearningProgressByUserAndChapter(
            @PathVariable Long userId, @PathVariable Long chapterId) {
        return ResponseEntity.ok(learningProgressService.getLearningProgressByUserAndChapter(userId, chapterId));
    }

    @PutMapping("/{id}")
    public ResponseEntity<LearningProgress> updateLearningProgress(
            @PathVariable Long id, @RequestBody LearningProgress learningProgress) {
        learningProgress.setId(id);
        return ResponseEntity.ok(learningProgressService.updateLearningProgress(learningProgress));
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteLearningProgress(@PathVariable Long id) {
        learningProgressService.deleteLearningProgress(id);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/user/{userId}/course/{courseId}/progress")
    public ResponseEntity<Double> calculateCourseProgress(
            @PathVariable Long userId, @PathVariable Long courseId) {
        return ResponseEntity.ok(learningProgressService.calculateCourseProgress(userId, courseId));
    }

    @GetMapping("/user/{userId}/chapter/{chapterId}/progress")
    public ResponseEntity<Double> calculateChapterProgress(
            @PathVariable Long userId, @PathVariable Long chapterId) {
        return ResponseEntity.ok(learningProgressService.calculateChapterProgress(userId, chapterId));
    }
} 